package cn.miju.alipay.handler;

import cn.miju.alipay.request.AlipayCloseOrderReq;
import cn.miju.alipay.response.AlipayBaseResp;
import cn.miju.common.content.CodeMessage;
import cn.miju.common.util.ValidationUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayResponse;
import com.alipay.api.internal.util.StringUtils;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * <pre>
 * 业务类
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-03-10 21:46
 */
@Slf4j
@Component
public class AlipayCloseOrderHandler extends AbstractAlipayHandler<AlipayCloseOrderReq, AlipayBaseResp> {
    /**
     * 可选返回参数(如果接口所需参数不满足需求，可按照官方接口文档，原样传入对应参数)
     */
    private Set<String> optionalRespParams;
    /**
     * 请求参数前置处理
     *
     * @param req 请求参数
     * @return 结果
     */
    @Override
    public AlipayTradeCloseRequest beforeRequest(AlipayCloseOrderReq req) {
        ValidationUtil.validate(req);
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", req.getOutTradeNo());
        optionalRespParams = req.getOptionalRespParams();
        super.setOptionalReqParams(bizContent, req.getOptionalReqParams());
        request.setBizContent(bizContent.toString());
        return request;
    }

    /**
     * 核心执行
     *
     * @param req {@link AlipayCloseOrderReq}
     * @return {@link AlipayBaseResp}
     */
    @Override
    public AlipayBaseResp doPlan(AlipayCloseOrderReq req) {
        AlipayTradeCloseRequest request = this.beforeRequest(req);
        AlipayClient alipayClient = super.createAlipayClient(req.getAlipayProperties());
        log.info("API--alipay.trade.close(统一收单交易关闭)-->Request：{}", JSON.toJSONString(request));
        AlipayTradeCloseResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            AlipayBaseResp baseResp = AlipayBaseResp.builder(CodeMessage.FAIL);
            log.error("API--alipay.trade.close(统一收单交易关闭)-->Exception：{}", e.getMessage(), e);
            return baseResp;
        }
        return this.afterResponse(response);
    }

    /**
     * 响应参数后置处理
     *
     * @param resp         响应参数
     * @return 结果
     */
    @SneakyThrows
    @Override
    public AlipayBaseResp afterResponse(AlipayResponse resp) {
        AlipayBaseResp baseResp = AlipayBaseResp.builder(CodeMessage.FAIL);
        AlipayTradeCloseResponse response = (AlipayTradeCloseResponse) resp;
        baseResp.setMsg(StringUtils.isEmpty(response.getSubMsg()) ? baseResp.getMsg() : response.getSubMsg());
        if (response.isSuccess()) {
            baseResp = AlipayBaseResp.builder(CodeMessage.SUCCESS);
            baseResp.setOptionalRespParams(super.getOptionalRespParams(response, optionalRespParams));
        }
        if (!StringUtils.isEmpty(response.getSubMsg()) && "ACQ.TRADE_NOT_EXIST".equals(response.getSubMsg())) {
            baseResp = AlipayBaseResp.builder(CodeMessage.TRADE_NOT_EXIT);
        }
        return baseResp;
    }
}
